﻿using DongFangGuoKai.TaskPlatform.Commn.ApiModule;
using DongFangGuoKai.TaskPlatform.Commn.ApiModule.Dto;
using DongFangGuoKai.TaskPlatform.Commn.Attributes;
using DongFangGuoKai.TaskPlatform.Commn.ConfigureOptions;
using DongFangGuoKai.TaskPlatform.Commn.LogModule;
using DongFangGuoKai.TaskPlatform.Job.OutRoundReturn.Model;
using DongFangGuoKai.TaskPlatform.Job.WarehousingReturn;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Quartz;
using RestSharp;
using SqlSugar;

namespace DongFangGuoKai.TaskPlatform.Job.OutRoundReturn
{
    [Job("出库任务", "出库回传", "出库回传任务3分钟执行一次")]
    public class OutRoundReturnJob : IJob
    {
        public IApiClient _apiClient { get; set; }
        public IOptions<UrlConfigOption> _urlOption { get; set; }
        public LogHelper<OutRoundReturnJob> _log { get; set; }

        public SyncIDType _syncID = SyncIDType.OutRoundOut;
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                UrlConfigOption? urlConfigOption = _urlOption.Value;
                string url = Path.Combine(urlConfigOption.WMSUrl, JobHelper.GetWMSSuffixUrl(_syncID));
                var result = _apiClient.Get<ApiDataResult<JToken>>(url);

                if (result.Code != 200)
                {
                    _log.LogError(url, null, result.Msg, result.Code.ToString());
                    await Task.CompletedTask;
                    return;
                }
                if (!result.Data.Any())
                {
                    await Task.CompletedTask;
                    return;
                }

                var data = result.Data.ToObject<OutRoundReturnModel>();
                #region v1.  实体字段一样，简化 
                //OutRoundReturnDto outRoundReturnDto = new OutRoundReturnDto()
                //{

                //    PickingNumber = data.PickingNumber,
                //    StoreName = data.StoreName,
                //    NeedDeptName = data.NeedDeptName
                //};
                //if (data.Items != null && data.Items.Count > 0)
                //{
                //    outRoundReturnDto.Items = new();
                //    foreach (var item in data.Items)
                //    {
                //        outRoundReturnDto.Items.Add(new  OutRoundReturnDetailDto()
                //        {
                //            Batch = item.Batch,
                //            Count = item.Count,
                //            RelationId = item.RelationId,
                //            Remark = item.Remark
                //        });
                //    }
                //}

                #endregion
                string postUrl = Path.Combine(urlConfigOption.HaiNingUrl, JobHelper.GetHHSuffixUrl(_syncID));
                var res = _apiClient.Post<ApiResultData>(postUrl, data);
                if (res.Code != "200")
                {
                    _log.LogError(postUrl, JsonConvert.SerializeObject(data), res.Msg);
                    await Task.CompletedTask;
                    return;
                }
                string putUrl = Path.Combine(urlConfigOption.WMSUrl, "TShippingOrderDetail/UpdoutShippingStatus");

                var para = new QueryParameter("OrderSn", data.PickingNumber);
                var putRes = _apiClient.Put<ApiDataResult<object>>(putUrl, new Parameter[1]
                        {
                       para
                        });
                if (putRes.Code != 200)
                {
                    _log.LogError(putUrl, "OrderSn:" + data.PickingNumber, "出库状态回传失败，详情:" + putRes.Msg, putRes.Code.ToString());
                    await Task.CompletedTask;
                    return;
                }
                _log.LogSuc(postUrl, JsonConvert.SerializeObject(data));
            }
            catch (Exception ex)
            {
                _log.LogCtmError(ex.Message);
            }


            await Task.CompletedTask;
        }
    }
}
